package com.safeneeds.linguimana.ser.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import com.safeneeds.linguimana.dto.PortConstant;
import com.safeneeds.linguimana.entity.EntityPortNews;
import com.safeneeds.linguimana.entity.PortNewsCate;
import com.safeneeds.linguimana.entity.PortNewsCateMap;
import com.safeneeds.linguimana.entity.dao.IPortNewsCateDao;
import com.safeneeds.linguimana.entity.dao.IPortNewsCateMapDao;
import com.safeneeds.linguimana.ser.IBgNewsCateService;
import com.safeneeds.sysmana.entity.SysDepa;
import com.safeneeds.util.base.IUserInfo;
import com.safeneeds.util.base.RetType;
import com.safeneeds.util.jsTree.JsTreeJson;
import com.safeneeds.util.jsTree.JsTreeState;
import com.safeneeds.util.jsTree.JsTreeUtil;
import com.safeneeds.util.jsTree.JsTreeUtil.IObjectToJsTree;
import com.safeneeds.util.other.StringUtil;

/**
 * 门户网站后台新闻管理
 * @author gaof
 * @date 2017年2月7日
 */
@Service
@Transactional
public class BgNewsCateService implements IBgNewsCateService {

	@Resource(name="portNewsCateDao")
	private IPortNewsCateDao portNewsCateDao;
	
	@Resource(name="portNewsCateMapDao")
	private IPortNewsCateMapDao portNewsCateMapDao;
	
	/**
	 * 获取新闻类分类列表
	 * @return
	 */
	@Override
	public RetType listNewsCate() {
		
		RetType retType = new RetType();
		
		//获取所有
		List<PortNewsCate> list = portNewsCateDao.listNewsCate();
		
		JsTreeJson jsTree = new JsTreeJson();
		jsTree.setId("0");//根节点
		jsTree.setText("新闻分类");
		JsTreeState state = new JsTreeState();
		state.setOpened(true);
		jsTree.setState(state);
		
		jsTree = JsTreeUtil.getJsTree(jsTree, list, new IObjectToJsTree() {
			
			@Override
			public JsTreeJson objectToJsTree(Object obj) {
				PortNewsCate req = (PortNewsCate) obj;
				JsTreeJson tree = getJsTreeJson(req);
				return tree;
			}
			
			@Override
			public String getObjectPId(Object obj) {
				PortNewsCate req = (PortNewsCate) obj;
				return String.valueOf(req.getPid());
			}
		});
		
		retType.doSuccess(jsTree);
		return retType;
	}
	
	/**
	 * 对象转jsTree数据
	 * @param req
	 * @return
	 */
	protected JsTreeJson getJsTreeJson(PortNewsCate req) {
		
		JsTreeJson tree = new JsTreeJson();
		tree.setId(req.getCateId());
		tree.setText(req.getName());
		//图标
		/*if(Constant.APP_REQ_FUNC_DET.equals(req.getType().toString())){//具体功能
			tree.setIcon("fa fa-file-text-o");
		}else if(Constant.APP_REQ_FUNC.equals(req.getType().toString())){//功能
			tree.setIcon("fa fa-list-ul");
		}else if(Constant.APP_REQ_MODEL.equals(req.getType().toString())){//模块
			tree.setIcon("fa fa-list-alt");
		}else{//其他
			tree.setIcon("fa fa-file");
		}*/
		//状态
		JsTreeState state = new JsTreeState();
		state.setOpened(true);
		tree.setState(state);
		//a标签附带参数
		tree.setA_attr(req);
		
		return tree;
	}

	/**
	 * 添加/编辑新闻分类
	 * @param cate
	 * @param userInfo
	 * @return
	 */
	@Override
	public RetType addNewsCate(PortNewsCate cate, IUserInfo userInfo) {
		
		RetType retType = null;
		
		if(StringUtil.isEmpty(cate.getCateId())){//新增
			
			retType = addNewsCateDo(cate, userInfo);
			
		}else{//编辑
			
			retType = updateNewsCateDo(cate, userInfo);
		}
		
		return retType;
	}

	/**
	 * 编辑新闻分类
	 * @param cate
	 * @param userInfo
	 * @return
	 */
	private RetType updateNewsCateDo(PortNewsCate cate, IUserInfo userInfo) {
		
		RetType retType = new RetType();
		
		PortNewsCate cateHis = portNewsCateDao.getNewsCateById(cate.getCateId());
		if(cateHis == null){
			retType.doError("新闻分类不存在，请刷新页面！");
			return retType;
		}
		
		cateHis.setCreateTime(new Date());
		cateHis.setCreateUser(userInfo.getUserName());
		cateHis.setName(cate.getName());
		cateHis.setPid(cate.getPid());
		cateHis.setSort(cate.getSort());
		
		//将得到值CateIndex 保存
		cateHis.setCateIndex(cate.getCateIndex());
		
		retType.doSuccess();
		return retType;
	}

	/**
	 * 新增新闻分类
	 * @param cate
	 * @param userInfo 
	 * @return
	 */
	private RetType addNewsCateDo(PortNewsCate cate, IUserInfo userInfo) {
		
		cate.setCanDel(PortConstant.NEWS_CATE_CAN_DEL_YES);//设置新闻分类默认可删除
		cate.setCreateTime(new Date());
		cate.setCreateUser(userInfo.getUserName());
		cate.setStatus(PortConstant.NEWS_CATE_STATUS_SHOW);//设置新闻分类默认显示
		portNewsCateDao.addNewsCate(cate);
		
		RetType retType = new RetType();
		retType.doSuccess();
		
		return retType;
	}

	/**
	 * 删除新闻分类
	 * @param id
	 * @return
	 */
	@Override
	public RetType delNewsCate(String id) {
		
		RetType retType = new RetType();
		List<PortNewsCate> cateList = null;
		
		if("0".equals(id)){//从根目录删除
			
			retType.doError("不能从跟目录删除分类！");
			return retType;
			//portNewsCateDao.delAllNewsCate();
			//portNewsCateDao.delAllNewsCateMap();
			
		}else{
			
			PortNewsCate cate = portNewsCateDao.getNewsCateById(id);
			if(cate == null){
				retType.doError("新闻分类不存在，请刷新页面！");
				return retType;
			}
			
			if(PortConstant.NEWS_CATE_CAN_DEL_NO.equals(cate.getCanDel())){
				retType.doError("该分类不能删除！");
				return retType;
			}
			
			cateList = new ArrayList<PortNewsCate>();
			cateList.add(cate);
			getAllChildCate(cate, cateList);
			
			//删除新闻分类
			portNewsCateDao.delNewsCateList(cateList);
			
			//删除新闻分类和新闻的对应关系
			for(PortNewsCate item : cateList){
				portNewsCateDao.delNewsCateMapByCateId(item.getCateId());
			}
			
		}
		
		retType.doSuccess();
		return retType;
	}

	/**
	 * 获取所有下级分类
	 * @param cate
	 * @return
	 */
	public void getAllChildCate(PortNewsCate cate, List<PortNewsCate> cateList) {
		
		List<PortNewsCate> childList = portNewsCateDao.listCateByPId(cate.getCateId());
		if(childList != null && !childList.isEmpty()){
			cateList.addAll(childList);
			
			for(PortNewsCate item : childList){
				getAllChildCate(item, cateList);
			}
		}
		
	}

	/**
	 * 通过分类id获取分类
	 * @param mainCateId
	 * @return
	 */
	@Override
	public PortNewsCate getNewsCateById(String cateId) {
		return portNewsCateDao.getNewsCateById(cateId);
	}

	/**
	 * 通过新闻id获取新闻对应分类
	 * @param newsId
	 * @return
	 */
	@Override
	public List<PortNewsCateMap> getNewsCateByNewsId(String newsId) {
		return portNewsCateMapDao.getNewsCateByNewsId(newsId);
	}

	/**
	 * 通过新闻id获取新闻分类信息
	 * @param newsId
	 * @return
	 */
	@Override
	public List getNewsCateMapNameList(String newsId) {
		return portNewsCateMapDao.getNewsCateMapNameList(newsId);
	}
	/**
	 *获取新闻分类信息
	 *@param i 个数
	 * @return
	 */
	@Override
	public List listNewsCate_1(int i) {
		List<PortNewsCate> list = portNewsCateDao.listCateByPId_1(i,PortConstant.NEWS_STATUS_TOP);
		return list;

	}
	/**
	 * 获取新闻分类专题栏目
	 * @param i 个数
	 * @param name 分类栏目
	 * @return
	 */
	@Override
	public List<PortNewsCate> getSpecialCate(int i,String name) {
		List<PortNewsCate> list=portNewsCateDao.getChildCateImage(i,name);
		return list;
	}
	/**
	 * 获取置中新闻分类栏目
	 * @param i 个数
	 * @return
	 */
	@Override
	public List listNewsCate_2(int i) {
		List<PortNewsCate> list = portNewsCateDao.listCateByPId_1(i,PortConstant.NEWS_STATUS_MID);
		return list;
	}
/**
 * @param fi 父类个数
 * @param ci 子类个数
 * @return
 */
	@Override
	public List<List<PortNewsCate>> getChildCate(int fi ,int ci) {
		List<PortNewsCate> midcate=portNewsCateDao.listCateByPId_1(fi,PortConstant.NEWS_STATUS_MID);
		Iterator<PortNewsCate> iterator = midcate.iterator(); 
		List<List<PortNewsCate>> list=new ArrayList<List<PortNewsCate>>();
	    while(iterator.hasNext()){  
	    	list.add(portNewsCateDao.getChildCateImage(ci,iterator.next().getName()));
	    }
		
		return list;
	}
	

}
